package red.lsk.business.service.impl;

import java.beans.Transient;
import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import red.lsk.business.domain.BizRechargeRecords;
import red.lsk.business.domain.BizTransactionRecords;
import red.lsk.business.domain.dto.ConsumeDto;
import red.lsk.business.domain.vo.StatisticalReportVO;
import red.lsk.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.lsk.business.mapper.BizMemberMapper;
import red.lsk.business.domain.BizMember;
import red.lsk.business.service.IBizMemberService;
import red.lsk.common.core.text.Convert;

/**
 * 会员管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-09
 */
@Service
public class BizMemberServiceImpl implements IBizMemberService 
{
    @Autowired
    private BizMemberMapper bizMemberMapper;
    @Autowired
    private BizDiscountConfigServiceImpl bizDiscountConfigService;
    @Autowired
    private BizRechargeRecordsServiceImpl bizRechargeRecordsService;
    @Autowired
    private BizTransactionRecordsServiceImpl bizTransactionRecordsService;

    /**
     * 查询会员管理
     * 
     * @param id 会员管理主键
     * @return 会员管理
     */
    @Override
    public BizMember selectBizMemberById(String id)
    {
        return bizMemberMapper.selectBizMemberById(id);
    }

    /**
     * 查询会员管理列表
     * 
     * @param bizMember 会员管理
     * @return 会员管理
     */
    @Override
    public List<BizMember> selectBizMemberList(BizMember bizMember)
    {
        return bizMemberMapper.selectBizMemberList(bizMember);
    }

    /**
     * 新增会员管理
     * 
     * @param bizMember 会员管理
     * @return 结果
     */
    @Override
    public int insertBizMember(BizMember bizMember)
    {
        String idStr = IdUtil.getSnowflake(1, 1).nextIdStr();
        bizMember.setId(idStr);
        //注册时间
        bizMember.setCreateTime(DateUtils.getNowDate());
        //账户余额 总消费 目前折扣比例 赋初始值
        bizMember.setBalance(new BigDecimal(0));
        bizMember.setTotalConsumption(new BigDecimal(0));
        bizMember.setDiscountRatio(new BigDecimal(100));

        return bizMemberMapper.insertBizMember(bizMember);
    }

    /**
     * 修改会员管理
     * 
     * @param bizMember 会员管理
     * @return 结果
     */
    @Override
    public int updateBizMember(BizMember bizMember)
    {
        bizMember.setUpdateTime(DateUtils.getNowDate());
        return bizMemberMapper.updateBizMember(bizMember);
    }

    /**
     * 批量删除会员管理
     * 
     * @param ids 需要删除的会员管理主键
     * @return 结果
     */
    @Override
    public int deleteBizMemberByIds(String ids)
    {
        return bizMemberMapper.deleteBizMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员管理信息
     * 
     * @param id 会员管理主键
     * @return 结果
     */
    @Override
    public int deleteBizMemberById(String id)
    {
        return bizMemberMapper.deleteBizMemberById(id);
    }
    /**
     * 查找手机号码是否重复
     *
     */
    @Override
    public int countMemberByPhone(String phone){
        return bizMemberMapper.countMemberByPhone(phone);
    }

    /**
     * 根据手机号码给会员充值,并且更新会员余额、折扣、积分等
     */
    @Override
    @Transient
    public int rechargeMemberByPhone(String phone, BigDecimal money){
        /*
        1. 根据手机号码查找会员
        2. 查询充值金额与折扣的关系
        3. 更新会员余额、折扣、积分等
        4. 入充值记录表
         */
        BizMember bizMember = bizMemberMapper.selectMemberByPhone(phone);
        if(bizMember == null){
            throw new RuntimeException("手机号码不存在");
        }
        //查询此充值金额对应的折扣比例
        BigDecimal discountRatio = bizDiscountConfigService.selectBizDiscountConfigByMoney(money);
        //更新会员余额、折扣等
        bizMember.setBalance(bizMember.getBalance().add(money));
        bizMember.setDiscountRatio(discountRatio);
        bizMemberMapper.updateBizMember(bizMember);
        //入充值记录表
        BizRechargeRecords bizRechargeRecords = new BizRechargeRecords();
        bizRechargeRecords.setMemberId(bizMember.getId());
        bizRechargeRecords.setMemberPhone(phone);
        bizRechargeRecords.setRechargeAmount(money);
        bizRechargeRecords.setRechargeInfo("充值金额："+money+"，折扣比例："+discountRatio);
        bizRechargeRecords.setMemberBalance(bizMember.getBalance());
        bizRechargeRecords.setDiscountRatio(discountRatio);
        bizRechargeRecords.setCreateTime(DateUtils.getNowDate());
        bizRechargeRecordsService.insertBizRechargeRecords(bizRechargeRecords);
        return 1;
    }

    /**
     * 根据会员编号给会员充值,并且更新会员余额、折扣、积分等
     */
    @Override
    @Transient
    public int rechargeMemberByMemberId(String memberId, BigDecimal money,String remark){
                /*
        1. 查询充值金额与折扣的关系
        2. 更新会员余额、折扣、积分等
        3. 入充值记录表
         */
        BizMember bizMember = bizMemberMapper.selectBizMemberById(memberId);
        BigDecimal currentRation = bizMember.getDiscountRatio();
        //查询此充值金额对应的折扣比例
        BigDecimal sysRatio = bizDiscountConfigService.selectBizDiscountConfigByMoney(money);
        //在sysRatio和currentRation中取最小值
        BigDecimal discountRatio = sysRatio.compareTo(currentRation) < 0 ? sysRatio : currentRation;
        //更新会员余额、折扣等
        bizMember.setBalance(bizMember.getBalance().add(money));
        bizMember.setDiscountRatio(discountRatio);
        bizMemberMapper.updateBizMember(bizMember);
        //入充值记录表
        BizRechargeRecords bizRechargeRecords = new BizRechargeRecords();
        bizRechargeRecords.setMemberId(bizMember.getId());
        bizRechargeRecords.setMemberPhone(bizMember.getPhone());
        bizRechargeRecords.setRechargeAmount(money);
        bizRechargeRecords.setRechargeInfo("充值金额："+money+"，折扣比例："+discountRatio);
        bizRechargeRecords.setMemberBalance(bizMember.getBalance());
        bizRechargeRecords.setDiscountRatio(discountRatio);
        bizRechargeRecords.setCreateTime(DateUtils.getNowDate());
        bizRechargeRecords.setRemark(remark);
        bizRechargeRecordsService.insertBizRechargeRecords(bizRechargeRecords);
        return 1;
    }
    /**
     * 消费 输入原价 根据当前用户的折扣比例来计算
     */
    @Override
    @Transient
    public int consumeByMemberId(ConsumeDto consumeDto){
         /*
        1. 查询充值金额与折扣的关系
        2. 更新会员余额、折扣、积分等
        3. 入消费记录表
         */
        String memberId=consumeDto.getMemberId();
        BigDecimal money = consumeDto.getMoney();
        String remark = consumeDto.getRemark();
        //前端传参折扣
        BigDecimal tempDiscountRatio = consumeDto.getDiscountRatio();
        BizMember bizMember = bizMemberMapper.selectBizMemberById(memberId);
        BigDecimal balance = bizMember.getBalance();
        BigDecimal discountRatio = bizMember.getDiscountRatio();
        if(balance.compareTo(money) < 0){
            throw new RuntimeException("余额不足");
        }
        //计算实际费用 保留两位小数
        //此处用前端传过来的折扣来计算
        BigDecimal actualMoney = money.multiply(tempDiscountRatio).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
        bizMember.setTotalConsumption(bizMember.getTotalConsumption().add(money));
        bizMember.setLastConsumeTime(DateUtils.getNowDate());
        bizMember.setBalance(balance.subtract(actualMoney));
        bizMemberMapper.updateBizMember(bizMember);
        //入交易记录表
        BizTransactionRecords bizTransactionRecords = new BizTransactionRecords();
        bizTransactionRecords.setMemberId(bizMember.getId());
        bizTransactionRecords.setMemberPhone(bizMember.getPhone());
        bizTransactionRecords.setTransactionAmount(actualMoney);
        bizTransactionRecords.setMemberBalance(bizMember.getBalance());
        bizTransactionRecords.setTransactionInfo("消费金额："+money+"，折扣比例："+tempDiscountRatio+"，实际消费金额："+actualMoney);
        bizTransactionRecords.setCreateTime(DateUtils.getNowDate());
        bizTransactionRecords.setRemark(remark);
        bizTransactionRecords.setTransactionName(remark);
        bizTransactionRecordsService.insertBizTransactionRecords(bizTransactionRecords);
        return 1;
    }
    /**
     * 1.总会员数2.总交易金额3.今日新增会员数4.今日交易总额
     * 首页统计
     */
    public StatisticalReportVO getStatisticalReport(){
        int allMember = bizMemberMapper.countAllMember();
        BigDecimal allMoney = bizMemberMapper.countAllMoney();
        int todayMember = bizMemberMapper.countTodayMember();
        BigDecimal todayMoney = bizMemberMapper.countTodayMoney();

        BigDecimal countMonthMoney = bizMemberMapper.countMonthMoney();

        BigDecimal countMonthRecharge = bizMemberMapper.countMonthRecharge();


        StatisticalReportVO statisticalReportVO = new StatisticalReportVO(allMember, todayMember, allMoney, todayMoney);
        statisticalReportVO.setMonthMoney(countMonthMoney);
        statisticalReportVO.setMonthRecharge(countMonthRecharge);
        return statisticalReportVO;


    }
}
